package com.iuie.ns.system.service.setting.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.utils.ListUtils;
import com.iuie.ns.frame.utils.ShiroUtils;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.system.mapper.setting.SysUserSettingMapper;
import com.iuie.ns.system.service.resource.ISysResourceService;
import com.iuie.ns.system.service.setting.ISysUserSettingService;
import com.iuie.ns.system.service.setting.ISysUserSettingTemplateService;
import com.iuie.ns.system.vo.resource.SysResourceVO;
import com.iuie.ns.system.vo.role.SysRoleVO;
import com.iuie.ns.system.vo.setting.SysUserSettingVO;
import com.iuie.ns.system.vo.user.SysUserVO;

@Service("userSettingService")
public class SysUserSettingServiceImpl implements ISysUserSettingService {
	
	public static final String RES_TYPE = "userSetting";
	
	private static final Logger logger = LoggerFactory.getLogger(SysUserSettingServiceImpl.class);
	
	@Resource
	private ISysResourceService resourceService;
	
	@Resource
	private SysUserSettingMapper userSettingMapper;

	@Resource
	private ISysUserSettingTemplateService userSettingTemplateService;
	
	@Override
	public List<SysUserSettingVO> findAll() {
		SysUserVO user = (SysUserVO) ShiroUtils.getCurrentUser();
		String userId = user.getUserId();
		SysRoleVO currentRole = user.getCurrentRole();
		String roleId = currentRole.getRoleId();
//		String userId = "10000001";
//		String roleId = "4623C3E1E75F42F7B4593876007E20FA";
		List<SysUserSettingVO> list = userSettingMapper.findByUserId(userId, roleId);
		return list;
	}

	@Override
	public void saveAll(List<SysUserSettingVO> list) {
		// 1.删除历史设置
		SysUserVO user = (SysUserVO) ShiroUtils.getCurrentUser();
		String userId = user.getUserId();
		SysRoleVO currentRole = user.getCurrentRole();
		String roleId = currentRole.getRoleId();
//		String userId = "10000001";
//		String roleId = "4623C3E1E75F42F7B4593876007E20FA";
		this.deleteUserSettingByUserId(userId, roleId);
		
		// 2.保存当前设置
		if (ListUtils.isEmpty(list)) {
			return ;
		}
		for (SysUserSettingVO setting : list) {
			setting.setId(StringUtils.uuid());
			setting.setRoleId(roleId);
			setting.setUserId(userId);
			SysResourceVO template = userSettingTemplateService.findOneBySettingKey(setting.getSettingKey());
			
			logger.info("正在保存用户设置：settingKey={}, settingValue={}, 模板={}", setting.getSettingKey(), setting.getSettingValue(), template);
			if (template != null && StringUtils.isNotBlank(template.getResName())) {
				setting.setSettingName(template.getResName());
			} else {
				throw new ServiceException("保存失败，模板名称不存在");
			}
		}
		userSettingMapper.saveUserSettings(list);
		
		// 针对多值的用户设置，保存值
		for (SysUserSettingVO item : list) {
			String settingValue = item.getSettingValue();
			if (StringUtils.equals(item.getValueType(), "02") && StringUtils.isNotBlank(settingValue)) {
				String[] values = StringUtils.split(settingValue, ",");
				List<String> listValues = ListUtils.arrayToList(values);
				if (listValues == null || listValues.size() <= 0) {
					continue;
				}
				userSettingMapper.saveUserSettingValues(item.getId(), listValues);
			}
		}
	}

	@Override
	public Integer deleteUserSettingByUserId(String userId, String roleId) {
		List<SysUserSettingVO> list = this.findAll();
		if (ListUtils.isEmpty(list)) {
			return 0;
		}
		for (SysUserSettingVO setting: list) {
			// 多值情况下，先删除值
			if (StringUtils.equals(setting.getValueType(), "02")) {
				userSettingMapper.deleteSettingValues(setting.getId());
			}
		}
		Integer count = userSettingMapper.deleteUserSettingByUserId(userId);
		return count;
	}
	
	@Override
	public Integer deleteUserSettingsBySettingKey(String settingKey, String valueType) {
		if (StringUtils.equals(valueType, "02")) {
			
		}
		return null;
	}
	
	@Override
	public List<String> findCurrentRoleSettingKeys() {
		SysUserVO user = (SysUserVO) ShiroUtils.getCurrentUser();
		SysRoleVO currentRole = user.getCurrentRole();
		String roleId = currentRole.getRoleId();
		List<String> list = this.findSettingKeysByRoleId(roleId);
		return list;
	}
	
	@Override
	public List<String> findSettingKeysByRoleId(String roleId) {
		return userSettingMapper.findSettingKeysByRoleId(roleId);
	}

	@Override
	public List<SysUserSettingVO> findByUserId(String userId, String roleId) {
		return userSettingMapper.findByUserId(userId, roleId);
	}

	@Override
	public SysUserSettingVO findOneBySettingKey(String settingKey) {
		SysUserVO user = (SysUserVO) ShiroUtils.getCurrentUser();
		String userId = user.getUserId();
		SysRoleVO currentRole = user.getCurrentRole();
		String roleId = currentRole.getRoleId();
//		String userId = "10000001";
//		String roleId = "4623C3E1E75F42F7B4593876007E20FA";
		SysUserSettingVO setting = userSettingMapper.findOneBySettingKey(settingKey, roleId, userId);
		return setting;
	}

}
